package com.yamaha.bikedesalon;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.sromku.simple.fb.SimpleFacebook;

public abstract class AMyActivity extends AActivity
		implements
			View.OnClickListener {

	protected ImageLoader imageLoader;
	protected LayoutInflater inflater;

	private final MenuType menuType;
	private boolean isCreated = false;

	public AMyActivity(final MenuType menuType) {
		this.menuType = menuType;
		if (isCreated) {
			hideInputText();
		}
	}

	@Override
	public void onBackPressed() {

		final RelativeLayout menuPanel = (RelativeLayout) findViewById(R.id.menu_panel);
		final RelativeLayout headerPanel = (RelativeLayout) findViewById(R.id.header_panel);
		final ImageButton button = (ImageButton) headerPanel.findViewById(R.id.menu_logo);

		if (menuPanel.getVisibility() == View.VISIBLE) {
			menuPanel.setVisibility(View.GONE);
			button.setVisibility(View.VISIBLE);
			return;
		} else {
			overridePendingTransition(0, 0);
			super.onBackPressed();
		}
	}

	protected abstract List<EditText> getEditTexts();

	@Override
	public void onClick(View v) {

		hideInputText();

		if (onMenuClick(v)) {
			return;
		}

		onMyClick(v);
	}

	public abstract void onMyClick(View v);

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getLayoutId());

		simpleFacebook = SimpleFacebook.getInstance(this);
		if (!simpleFacebook.isLogin()) {
			LogUtil.w("Facebook", "User is NOT Login and back to Login Page.");

			final Intent intent = new Intent(this, LoginActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			startActivity(intent);
			return;
		}

		this.inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		final DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).build();
		final ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.getApplicationContext()).defaultDisplayImageOptions(defaultOptions).build();
		if ((null != this.imageLoader) && this.imageLoader.isInited()) {
			this.imageLoader.destroy();
			this.imageLoader = null;
		}
		this.imageLoader = ImageLoader.getInstance();
		if (!this.imageLoader.isInited()) {
			this.imageLoader.init(config);
		}

		setListenerForMenu();

		this.onMyCreated(savedInstanceState);

		isCreated = true;

	}

	protected abstract int getLayoutId();
	protected abstract void onMyCreated(final Bundle savedInstanceState);

	protected void hideInputText() {

		final List<EditText> editTexts = getEditTexts();

		if (null != editTexts) {

			for (final EditText editText : editTexts) {
				if (null != editText) {
					final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
				}
			}
		}
	}

	protected void showInputText(final EditText editText) {
		final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.showSoftInputFromInputMethod(editText.getWindowToken(), InputMethodManager.SHOW_IMPLICIT);
		imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
	}

	protected void setListenerForMenu() {

		final RelativeLayout menuPanel = (RelativeLayout) findViewById(R.id.menu_panel);

		final RelativeLayout headerPanel = (RelativeLayout) findViewById(R.id.header_panel);
		final ImageButton button = (ImageButton) headerPanel.findViewById(R.id.menu_logo);

		menuPanel.bringToFront();

		final ImageButton menuFindPart = (ImageButton) findViewById(R.id.menu_findpart);
		final ImageButton menuPostPart = (ImageButton) findViewById(R.id.menu_postpart);
		final ImageButton menuBooking = (ImageButton) findViewById(R.id.menu_booking);
		final ImageButton menuNotification = (ImageButton) findViewById(R.id.menu_notification);

		menuPanel.setOnClickListener(this);
		button.setOnClickListener(this);
		menuBooking.setOnClickListener(this);
		menuNotification.setOnClickListener(this);
		menuFindPart.setOnClickListener(this);
		menuPostPart.setOnClickListener(this);

		switch (this.menuType) {
			case BOOKING :
				break;
			case FIND_PART :
				break;
			case MAIN :
				break;
			case NOTIFICATION :
				break;
			case POST_PART :
				break;
			default :
				break;
		}

	}
	public boolean onMenuClick(final View v) {

		LogUtil.v(getClass().getSimpleName(), "Click View Id : " + v.getId());
		// Hide Keyboard
		hideInputText();

		final RelativeLayout menuPanel = (RelativeLayout) findViewById(R.id.menu_panel);
		final ImageButton menuLogo = (ImageButton) findViewById(R.id.menu_logo);

		switch (v.getId()) {
			case R.id.menu_logo : {
				if (menuPanel.getVisibility() == View.VISIBLE) {
					menuPanel.setVisibility(View.GONE);
					menuLogo.setVisibility(View.VISIBLE);
				} else {
					menuPanel.setVisibility(View.VISIBLE);
					menuLogo.setVisibility(View.GONE);
				}
			}
				return true;
			case R.id.menu_panel : {
				menuPanel.setVisibility(View.GONE);
				menuLogo.setVisibility(View.VISIBLE);
			}
				return true;
			case R.id.menu_booking : {
				if (this.menuType != MenuType.BOOKING) {
					menuPanel.setVisibility(View.GONE);
					menuLogo.setVisibility(View.VISIBLE);
					final Intent intent = new Intent(this, BookingFragment.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);
				}
			}
				return true;
			case R.id.menu_notification : {
				if (this.menuType != MenuType.NOTIFICATION) {
					menuPanel.setVisibility(View.GONE);
					menuLogo.setVisibility(View.VISIBLE);
					final Intent intent = new Intent(this, NotificationFragment.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);
				}
			}
				return true;
			case R.id.menu_findpart : {
				if (this.menuType != MenuType.FIND_PART) {
					menuPanel.setVisibility(View.GONE);
					menuLogo.setVisibility(View.VISIBLE);
					final Intent intent = new Intent(this, FindPartFragment.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);
				}
			}
				return true;
			case R.id.menu_postpart : {
				if (this.menuType != MenuType.POST_PART) {
					menuPanel.setVisibility(View.GONE);
					menuLogo.setVisibility(View.VISIBLE);
					final Intent intent = new Intent(this, PostPartFragment.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivity(intent);
				}
			}
				return true;
		}

		return false;
	}

}
